package com.hp.gxw.u8.xml;

import com.hp.gxw.u8.dto.SodetailsParameter;
import com.hp.gxw.u8.dto.SomainParameter;
import com.hp.gxw.utils.DateUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;

import java.util.Date;


public class SomainXml {
	
	public static String createXML(SomainParameter somainParameter) throws Exception{
		try {
			// 1、创建document对象
			Document document = DocumentHelper.createDocument();
			// 2、创建根节点rss
			Element ufinterface = document.addElement("ufinterface");
			ufinterface.addNamespace("z", "http://www.w3.org/2001/XMLSchema");
			// 向ufinterface节点添加efserverid属性(U8账套)
			ufinterface.addAttribute("efserverid", somainParameter.getEfserverid());
			// 向ufinterface节点添加eftype属性(方法：24代表采购入库)
			ufinterface.addAttribute("eftype", somainParameter.getEftype());
			// 向ufinterface节点添加proc属性(增删改查事件)
			ufinterface.addAttribute("proc", somainParameter.getProc());
			// 向ufinterface节点添加efdebug属性
			ufinterface.addAttribute("efdebug", "1");
			// 向ufinterface节点添加actions属性,入库后自动审核
//			ufinterface.addAttribute("actions", "verify");
			ufinterface.addAttribute("actions", "verify");
			ufinterface.addAttribute("ddate", DateUtil.getDateFormat(new Date()));
			// 4、生成子节点及子节点内容
			Element voucher = ufinterface.addElement("voucher");
			voucher.addAttribute("cardnumber", somainParameter.getEftype());
			voucher.addAttribute("ccardname", "");
			voucher.addNamespace("z", "EF");
			Element head = voucher.addElement("head");
			Element headRow = head.addElement("z:row");
			headRow.addAttribute("csocode", somainParameter.getCsocode());// 传入参数
			headRow.addAttribute("cbustype", somainParameter.getCbustype());// 传入参数
			headRow.addAttribute("cstcode", somainParameter.getCstcode());// 传入参数
			headRow.addAttribute("ddate", DateUtil.getDateFormat(somainParameter.getDdate()));// 传入参数
			headRow.addAttribute("ccuscode", somainParameter.getCcuscode());// 传入参数
			if(somainParameter.getCdepcode() != null){
				headRow.addAttribute("cdepcode", somainParameter.getCdepcode());// 传入参数：仓库
			}
			headRow.addAttribute("cexch_name", somainParameter.getCexch_name());// 传入参数：到货单号
			if(somainParameter.getCpersoncode() != null){
				headRow.addAttribute("cpersoncode", somainParameter.getCpersoncode());// 传入参数：仓库
			}
			headRow.addAttribute("iexchrate", somainParameter.getIexchrate());// 传入参数：到货单号
			headRow.addAttribute("cmaker", somainParameter.getCmaker());// 传入参数：到货单号
			if(somainParameter.getCverifier() != null){
				headRow.addAttribute("cverifier", somainParameter.getCverifier());// 传入参数：到货单号
				headRow.addAttribute("istatus", "1");// 传入参数：到货单号
			}
			if(somainParameter.getDverifydate() != null){
				headRow.addAttribute("dverifydate", DateUtil.getDateFormat(somainParameter.getDverifydate()));// 传入参数：到货单号
			}
			if(somainParameter.getDverifysystime() != null){
				headRow.addAttribute("dverifysystime", DateUtil.getDateTimeFormat(somainParameter.getDverifysystime()));// 传入参数：到货单号
			}
			headRow.addAttribute("cdefine11", somainParameter.getOrderNo());// 传入参数：MES销售单号
			headRow.addAttribute("ivtid", "95");// 传入参数：单据模板号
			Element body = voucher.addElement("body");
			int rowno = 1;
			for (SodetailsParameter sodetailsParameter : somainParameter.getSodetailsParameterList()) {
				Element bodyRow = body.addElement("z:row");
				bodyRow.addAttribute("cinvcode", sodetailsParameter.getCinvcode());// 传入参数：存货编码
				bodyRow.addAttribute("iquantity", sodetailsParameter.getIquantity().toString());// 传入参数：数量
				if(sodetailsParameter.getItaxunitprice() != null){
					bodyRow.addAttribute("itaxunitprice", sodetailsParameter.getItaxunitprice().toString());// 传入参数：数量
				}
				if(sodetailsParameter.getIunitprice() != null){
					bodyRow.addAttribute("iunitprice", sodetailsParameter.getIunitprice().toString());// 传入参数：数量
				}
				if(sodetailsParameter.getImoney() != null){
					bodyRow.addAttribute("imoney", sodetailsParameter.getImoney().toString());// 传入参数：数量
				}
				if(sodetailsParameter.getItax() != null){
					bodyRow.addAttribute("itax", sodetailsParameter.getItax().toString());// 传入参数：数量
				}
				if(sodetailsParameter.getIsum() != null){
					bodyRow.addAttribute("isum", sodetailsParameter.getIsum().toString());// 传入参数：数量
				}
				if(sodetailsParameter.getItaxrate() != null){
					bodyRow.addAttribute("itaxrate", sodetailsParameter.getItaxrate().toString());// 传入参数：数量
				}
				if(sodetailsParameter.getInatunitprice() != null){
					bodyRow.addAttribute("inatunitprice", sodetailsParameter.getInatunitprice().toString());// 传入参数：数量
				}
				if(sodetailsParameter.getInatmoney() != null){
					bodyRow.addAttribute("inatmoney", sodetailsParameter.getInatmoney().toString());// 传入参数：数量
				}
				if(sodetailsParameter.getInattax() != null){
					bodyRow.addAttribute("inattax", sodetailsParameter.getInattax().toString());// 传入参数：数量
				}
				if(sodetailsParameter.getInatsum() != null){
					bodyRow.addAttribute("inatsum", sodetailsParameter.getInatsum().toString());// 传入参数：数量
				}
				bodyRow.addAttribute("dpredate", DateUtil.getDateTimeFormat(sodetailsParameter.getDpredate()));// 传入参数：采购订单号
				bodyRow.addAttribute("dpremodate", DateUtil.getDateTimeFormat(sodetailsParameter.getDpremodate()));// 传入参数：采购订单号
				bodyRow.addAttribute("cgroupcode", sodetailsParameter.getCgroupcode());// 传入参数：采购订单号
				bodyRow.addAttribute("irowno", String.valueOf(rowno));// 行号
				bodyRow.addAttribute("cdefine28", sodetailsParameter.getDetailId());// 行号
				rowno++;
			}
			
			OutputFormat format = OutputFormat.createPrettyPrint();
			// 设置编码格式
			format.setEncoding("UTF-8");
			
			return document.asXML();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
